  USE scm_main;
  -- ��Ҫ�ȴ�������ַ����洢���̣�string_split��
  if exists (select 1
            from sysobjects
            where  id = object_id('bd_door_expand_remove')
            and type in ('P','PC'))
  DROP PROCEDURE bd_door_expand_remove
  GO
  CREATE PROCEDURE bd_door_expand_remove
    @xh_str varchar(max),
    @ip varchar(200),
    @operator varchar(200)
  as
  BEGIN

  -- �رմ�ӡ��Ӱ������
  SET NOCOUNT ON;

  -- ���巵����Ӱ���������
    declare @result int = 0;

      -- ���������ű�
      CREATE TABLE #xh_bh(
          [xh] nvarchar(50) COLLATE Chinese_PRC_CI_AS  NULL
      );

      -- �������
      insert into #xh_bh(xh)
      select items from string_split(@xh_str,',');


    --  �쳣��׽����
      BEGIN TRY

      -- ��������
        BEGIN TRAN

        declare @sum int = 0;
        declare @index int = 0;
        declare @xh bigint = 0;
        declare @door_bh bigint = 0;
        declare @door_name varchar(200);
        declare @dept_name varchar(200);
        declare @dev_serial varchar(200);

        select @sum = count(0) from #xh_bh

        WHILE (@index < @sum) BEGIN

          -- �Ȳ�ѯȨ�޵���Ϣ
          select @xh = isnull(b.xh,0),@door_bh = isnull(r.gate_bh,''),@door_name = isnull(d.door_name,''),@dept_name = isnull(p.dep_name,'')
          from MJ_GATE_LEVEL_real r
          inner join #xh_bh b on r.xh = b.xh
          inner join st_door_real d on r.gate_bh = d.bh
          left join dt_dep p on r.dep_serial = p.dep_serial
          order by b.xh offset 0 rows fetch next 1 rows only

          -- ɾ��Ȩ��
          delete from #xh_bh where xh = @xh --Ϊʲôɾ�� #xh_bh ���ݣ�	��Ϊ����MJ_GATE_LEVEL_real���xh��Ϊ������������ﲻɾ����MJ_GATE_LEVEL_realɾ������ô�������ݻ�����쳣
          delete from MJ_GATE_LEVEL_real where gate_bh = @door_bh

          -- ��ѯ�Ź����豸����Ϣ
          select @dev_serial = isnull(dev_serial,'') from st_doorfx_real where gate_bh = @door_bh

          -- ֪ͨ�豸��ȡ 1073741824 ��Ȩ�Ĺ涨��ʽ
          insert into jreal_update_0(jdev_bh,jdodata) VALUES (@dev_serial,1073741824)
          insert into jreal_update_1(jdev_bh,jdodata) VALUES (@dev_serial,1073741824)

          -- ����������־
          insert into wt_mj_log_real(lx,log_type,log_state,log_bz,log_sj,log_ip,gly_no,regserial)
          VALUES (2,3,0,'��չ��Ȩ:ɾ��Logistics MOVE Office�ŵ���Ȩ,����Ϊ:'+@dept_name,GETDATE(),@ip,@operator,'')

          SET @index = @index + 1;
          SET @result = @index;
        END

      --  �ύ����
        COMMIT TRAN

    --  �����쳣��׽
      END TRY

    --  �쳣����
      BEGIN CATCH
        ROLLBACK TRAN;
      END CATCH


  --  �����ʱ��
    select @result as result
    drop table #xh_bh

  -- �򿪴�ӡ��Ӱ������
  SET NOCOUNT OFF;
  END;
